package com.hjq.controller;

import com.hjq.common.Result;
import com.hjq.entity.po.User;
import com.hjq.entity.vo.UserInfoForUpdateRequestVO;
import com.hjq.entity.vo.UserInfoForUpdateVO;
import com.hjq.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户管理
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 查询所有用户
     *
     * @return List<User>
     */
    @GetMapping
    public Result<List<User>> findAll() {
        List<User> users = userService.findAll();
        return Result.success(users);
    }

    /**
     * 删除用户
     *
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    public Result<Object> delete(@PathVariable Integer id) {
        userService.delete(id);
        return Result.success("成功删除");
    }

    /**
     * 修改用户
     *
     * @return 修改结果
     */
    @PutMapping
    public Result<Object> update(@RequestBody UserInfoForUpdateRequestVO user) {
        log.info("用户更新，{}",user);
        UserInfoForUpdateVO userInfoVO = userService.update(user);
        return Result.success(userInfoVO);
    }
    /**
     * 重置用户密码
     *
     * @return 登录用户信息
     */
    @PutMapping("/{id}")
    public Result<Object> resetPassword(@PathVariable Long id) {
        log.info("用户id：{}", id);
        Integer count = userService.resetPassword(id);
        return count == 1 ? Result.success("密码重置成功") : Result.error("密码重置失败");
    }


}
